import BaseOAuthProvider from './base-provider';
import type { OAuthConfig } from './base-provider';
import GoogleProvider from './google-provider';
import GitHubProvider from './github-provider';
import TwitterProvider from './twitter-provider';

// OAuth提供商类型
export type ProviderType = 'GOOGLE' | 'GITHUB' | 'TWITTER';

// OAuth提供商工厂类
export class OAuthProviderFactory {
  private static providers: Map<ProviderType, BaseOAuthProvider> = new Map();

  /**
   * 注册OAuth提供商
   */
  static registerProvider(type: ProviderType, provider: BaseOAuthProvider): void {
    this.providers.set(type, provider);
  }

  /**
   * 获取OAuth提供商
   */
  static getProvider(type: ProviderType): BaseOAuthProvider {
    const provider = this.providers.get(type);
    if (!provider) {
      throw new Error(`OAuth provider ${type} not registered`);
    }
    return provider;
  }

  /**
   * 创建OAuth提供商
   */
  static createProvider(type: ProviderType, config: OAuthConfig): BaseOAuthProvider {
    let provider: BaseOAuthProvider;

    switch (type) {
      case 'GOOGLE':
        provider = new GoogleProvider(config);
        break;
      case 'GITHUB':
        provider = new GitHubProvider(config);
        break;
      case 'TWITTER':
        provider = new TwitterProvider(config);
        break;
      default:
        throw new Error(`Unsupported OAuth provider: ${type}`);
    }

    this.registerProvider(type, provider);
    return provider;
  }

  /**
   * 获取所有已注册的提供商类型
   */
  static getRegisteredProviders(): ProviderType[] {
    return Array.from(this.providers.keys());
  }

  /**
   * 检查提供商是否已注册
   */
  static isProviderRegistered(type: ProviderType): boolean {
    return this.providers.has(type);
  }
}

export default OAuthProviderFactory;